package framework.service.transaction.impl;

import framework.dao.exception.DataAccessException;
import framework.dao.jdbc.util.JdbcUtil;
import framework.service.transaction.TransactionManager;

import java.sql.Connection;

public class TransactionManagerImpl implements TransactionManager {
    @Override
    public void begin() {
        Connection con=null;
        try {
            con= JdbcUtil.getConnection();

            con.setAutoCommit(false);
        } catch (Exception e){
            e.printStackTrace();
            throw new DataAccessException("数据访问失败",e);

        }
    }

    @Override
    public void commit() {
        Connection con=null;
        try {
            con= JdbcUtil.getConnection();

            con.commit();
        } catch (Exception e){
            e.printStackTrace();
            throw new DataAccessException("数据访问失败",e);

        } finally {
            JdbcUtil.close();
        }
    }

    @Override
    public void rollback() {
        Connection con=null;
        try {
            con= JdbcUtil.getConnection();

            con.rollback();
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            JdbcUtil.close();
        }
    }
}
